package com.example.webapi.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.webapi.Utils.ResponseModel;
import com.example.webapi.entity.Project;
import com.example.webapi.service.ProjectService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author  张彬 
 * @since 2021-10-28
 */
@RestController
@RequestMapping("/api")
public class ProjectController {
    @Resource
    ProjectService projectService;

    @PostMapping("save")
    public ResponseModel save(@RequestBody JSONObject data){
        Project project = new Project();
        project.setId(data.getStr("id"));
        project.setProjectName(data.getStr("projectName"));
        project.setFileNumber(data.getInt("fileNumber"));
//        project.setProblemNumber(data.getInt("problemNumber"));
//        project.setPeople(data.getStr("people"));
        project.setCreateTime(DateUtil.now());
        boolean result = projectService.save(project);
        if (result){
            Map<String,Object> map = projectService.getMap(new QueryWrapper<Project>().eq("id",project.getId()));
            return ResponseModel.success(map);
        }else {
            return ResponseModel.fail("新增失败");
        }
    }
    @GetMapping("list")
    public ResponseModel List(){
        List<Map<String,Object>> objectList = projectService.listMaps();
        return ResponseModel.success(objectList);
    }
}
